/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.actnode.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.actnode.entity.ActWorflow;
import com.jeeplus.modules.actnode.mapper.ActWorflowMapper;
import com.jeeplus.modules.actnode.entity.ActWorflownode;
import com.jeeplus.modules.actnode.mapper.ActWorflownodeMapper;

/**
 * 流程职责Service
 * @author 流程职责
 * @version 2023-10-19
 */
@Service
@Transactional(readOnly = true)
public class ActWorflowService extends CrudService<ActWorflowMapper, ActWorflow> {

	@Autowired
	private ActWorflownodeMapper actWorflownodeMapper;
	
	public ActWorflow get(String id) {
		ActWorflow actWorflow = super.get(id);
		actWorflow.setActWorflownodeList(actWorflownodeMapper.findList(new ActWorflownode(actWorflow)));
		return actWorflow;
	}
	
	public List<ActWorflow> findList(ActWorflow actWorflow) {
		return super.findList(actWorflow);
	}
	
	public Page<ActWorflow> findPage(Page<ActWorflow> page, ActWorflow actWorflow) {
		return super.findPage(page, actWorflow);
	}
	
	@Transactional(readOnly = false)
	public void save(ActWorflow actWorflow) {
		super.save(actWorflow);
		for (ActWorflownode actWorflownode : actWorflow.getActWorflownodeList()){
			if (actWorflownode.getId() == null){
				continue;
			}
			if (ActWorflownode.DEL_FLAG_NORMAL.equals(actWorflownode.getDelFlag())){
				if (StringUtils.isBlank(actWorflownode.getId())){
					actWorflownode.setUpper(actWorflow);
					actWorflownode.preInsert();
					actWorflownodeMapper.insert(actWorflownode);
				}else{
					actWorflownode.preUpdate();
					actWorflownodeMapper.update(actWorflownode);
				}
			}else{
				actWorflownodeMapper.delete(actWorflownode);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(ActWorflow actWorflow) {
		super.delete(actWorflow);
		actWorflownodeMapper.delete(new ActWorflownode(actWorflow));
	}
	
}